package com.guigu.qxl.controller;

import com.guigu.pojo.SysMenus;
import com.guigu.pojo.SysRoles;
import com.guigu.pojo.SysUsers;
import com.guigu.qxl.service.SysMenusService;
import com.guigu.qxl.service.SysRolesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("SysMenus")
public class SysMenusController {
    @Autowired
    SysMenusService sysMenuService;
    @Autowired
    SysRolesService showRoleByUsers;

    @RequestMapping("showMenuByUsers")
    public List<SysMenus> showMenuByUsers(SysRoles r, HttpSession session){
        SysUsers sysUsers= (SysUsers) session.getAttribute("sysUsers");
        List<SysRoles> role=showRoleByUsers.showRoleByUsers(sysUsers);
        if(r.getId()!=null && r.getId()!=0){
            role=new ArrayList<SysRoles>() {};
            role.add(r);
        }
        List<SysMenus> roleMenu=null;
        int i=0;
        for(SysRoles r2:role){
            if(i==0){
                roleMenu=sysMenuService.showMenuByRole(r2);
                i++;
            }else{
                List<SysMenus> listMenu=sysMenuService.showMenuByRole(r2);
                for(SysMenus m:listMenu){
                    if(!roleMenu.contains(m)){
                        roleMenu.add(m);
                    }
                }
            }
        }
        List<SysMenus> AllMenu=sysMenuService.showAllMenu();
        for(SysMenus m:AllMenu){
            if(roleMenu.contains(m)){
                m.setChecked(true);
            }
        }
        return AllMenu;
    }
}
